﻿using Microsoft.AspNetCore.Mvc;
using Snowflake.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using WisDom.SmartTMS.Dto;
using WisDom.SmartTMS.Entity;
using WisDom.SmartTMS.InterFace;
using System.Linq.Dynamic.Core;
using Microsoft.EntityFrameworkCore;

namespace WisDom.SmartTMS.Services
{
    /// <summary>
    /// 合同
    /// </summary>
    public class TMSContractCarrierService : SmartTMSAppService, ITMSContractCarrierService
    {
        private readonly IRepository<TMSContractCarrier, Guid> _repository;
        private readonly IRepository<Audit, Guid> _adt;//审核记录
        private readonly IRepository<AuditLog, Guid> _adtLog;//审核记录
        public TMSContractCarrierService(IRepository<TMSContractCarrier, Guid> repository, IRepository<Audit, Guid> adt, IRepository<AuditLog, Guid> adtLog)
        {
            _repository = repository;
            _adt = adt;
            _adtLog = adtLog;
        }
        /// <summary>
        /// 添加合同
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<TMSContractCarrierDto> AddCarrierAsync([FromBody]TMSContractCarrierDto data)
        {
            //拼接 创建合同单号
            var worker = new IdWorker(1, 1);
            long num = worker.NextId();
            String snum = "HT"+ num ; 


            TMSContractCarrier contract = new(
                    GuidGenerator.Create(),
                    DateTime.Now,
                    data.CreatorId,
                    data.Deleted,
                    snum,
                    data.ContractTitle,
                    data.CarriageUnit,
                    data.CarriageMan,
                    data.Line,
                    data.FreightRates,
                    data.CharterConditionTonnage,
                    data.CharterValue,
                    DateTime.Now,
                    data.Operator,
                    data.ContractMoney,
                    data.ProjectDescription, 


                    data.MainClause,
                    data.Accessory,
                    //添加默认状态为0  0为待审核
                    data.State=0,
                    data.Approver
                    ) ;
            var result = await _repository.InsertAsync(contract);
            //添加审核信息 记录
            Audit audit = new(
                GuidGenerator.Create(),
                data.Id,
                Enums.BusinessType.ContractCargoOwner,      //合同模块
                DateTime.Now,
                GuidGenerator.Create()
            );
            //审核记录日志
            audit.AddAuditLog(GuidGenerator.Create(), (Guid)CurrentUser.Id, data.Approver);

            await _adt.InsertAsync(audit); //持久化
            
            return ObjectMapper.Map<TMSContractCarrier, TMSContractCarrierDto>(result);
            

        }
        /// <summary>
        /// 删除合同
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task DeleteCarrierAsync(Guid ids)
        {
            await _repository.DeleteAsync(ids);
        }
        /// <summary>
        /// 显示合同
        /// </summary>
        /// <returns></returns>
        /// <summary>
        /// 显示合同
        /// </summary>
        /// <returns></returns>
        public async Task<PagedResultDto<TMSContractCarrierDto>> GetCarrierAsync(PagedAndSortedResultRequestDto input)
        {
            //var lst = await _repository.GetListAsync();
            //return ObjectMapper.Map<List<TMSContractCarrier>, List<TMSContractCarrierDto>>(lst);
            //创建一个查询对象
            var query = _repository.OrderBy(input.Sorting);

            //获取总条数
            var totalCount = await query.CountAsync();

            //从仓储获取实体数据对象
            List<TMSContractCarrier> vehicles = await query
                .Skip(input.SkipCount)
                .Take(input.MaxResultCount).ToListAsync();

            //将实体数据转换成Dto
            List<TMSContractCarrierDto> vehicleDtos =
                ObjectMapper.Map<List<TMSContractCarrier>, List<TMSContractCarrierDto>>(vehicles);

            //返回分页结果集
            return new PagedResultDto<TMSContractCarrierDto>(totalCount, vehicleDtos);
        }

        /// <summary>
        /// 获取一个合同
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<TMSContractCarrierDto> GetTheCarrierAsync( Guid id)
        {
            var res = await _repository.GetAsync(id);
            return ObjectMapper.Map<TMSContractCarrier, TMSContractCarrierDto>(res);
        }
        /// <summary>
        /// 修改合同
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task UpdateCarrierAsync(TMSContractCarrierDto data)
        {
            TMSContractCarrier res = ObjectMapper.Map<TMSContractCarrierDto, TMSContractCarrier>(data);
            await _repository.UpdateAsync(res);
        }
    }
}
